package com.wx.md.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wx.md.dao.SysApkVersionDao;
import com.wx.md.service.SysApkVersionService;
import com.wx.md.tb.SysApkVersion;
import com.wx.md.utils.QRCodeUtil;
import com.wx.md.utils.RandomUtils;
import com.wx.sftp.SaasFtp;
import com.wx.sftp.share.SaasDir;
import com.wx.sftp.utils.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.Serializable;
import java.util.Base64;
import java.util.Collection;
import java.util.List;

/**
 * (SysApkVersion)表服务实现类
 *
 * @author wx
 * @since 2021-07-05 10:04:34
 */
@Service("sysApkVersionService")
public class SysApkVersionServiceImpl extends ServiceImpl<SysApkVersionDao, SysApkVersion> implements SysApkVersionService {

    @Autowired
    private SaasFtp saasFtp;

    @Override
    public String getQrCodeUrl(String apkPath) {
        try {
            BufferedImage bufferedImage = QRCodeUtil.createImage(apkPath, "", true);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            ImageIO.write(bufferedImage, "png", stream);
            InputStream inputStream = new ByteArrayInputStream(stream.toByteArray());
            String fileName = RandomUtils.randomDateId() + ".png";
            String url = saasFtp.upload(inputStream, SaasDir.IMAGES, fileName);
            return url;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    @Override
    public SysApkVersion getCurrentApk() {
        QueryWrapper queryWrapperDb = new QueryWrapper();
        queryWrapperDb.orderByDesc("create_time");
        List<SysApkVersion> sysApkVersionList = super.list(queryWrapperDb);
        return sysApkVersionList.get(0);
    }

    @Override
    public boolean save(SysApkVersion entity) {
        entity.setId(RandomUtils.randomDateId());
        String qrCode = this.getQrCodeUrl(entity.getApkPath());
        entity.setQrCode(qrCode);
        return super.save(entity);
    }

    @Override
    public boolean updateById(SysApkVersion entity) {
        return super.updateById(entity);
    }

    @Override
    public SysApkVersion getById(Serializable id) {
        return super.getById(id);
    }

    @Override
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        return super.removeByIds(idList);
    }

    @Override
    public IPage<SysApkVersion> page(IPage<SysApkVersion> page, Wrapper<SysApkVersion> queryWrapper) {
        QueryWrapper queryWrapperDb = new QueryWrapper();
        queryWrapperDb.orderByDesc("create_time");
        return super.page(page, queryWrapperDb);
    }
}
